/**
 * 会员管理:用户管理js
 */
$(function () {
    $("#jqGrid").Grid({
        height: '700px',
        url: '../user/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 30, key: true, hidden: true},
            {label: '用户编号', name: 'id', index: 'id', width: 10},
            {label: '昵称', name: 'name', index: 'name', width: 20},
            {label: '手机号', name: 'mobile', index: 'mobile', width: 20},
            {label: '真实姓名', name: 'realName', index: 'realName', width: 30},
            {
                label: '性别', name: 'gender', index: 'gender', width: 10,
                formatter: function (value) {
                    if (value == 0) {
                        return '男';
                    } else {
                        return '女';
                    }
                }
            },
            {
                label: '会员类型', name: 'isdOne', index: 'isdOne', width: 10,
                formatter: function (value) {
                    if (value == 0) {
                        return '普通会员';
                    }
                    else if (value == 1) {
                        return 'D-one';
                    }
                    else if (value == 2) {
                        return '变异D-one';
                    }

                }
            }, {
                label: '冻结', name: 'status', index: 'status', width: 15,
                formatter: function (value) {
                    if (value == 1) {
                        return '否';
                    }
                    return '是';
                }
            },
            {label: '冻结原因', name: 'remark', index: 'remark', width: 30},
            {
                label: '头像', name: 'pictureUrl', index: 'pictureUrl', width: 20, formatter: function (value) {
                    return transImg(value);
                }
            },
            {
                label: '微信ID', name: 'wxUnionId', index: 'wxUnionId', width: 40
            }, {
                label: '级别', name: 'level', index: 'level', width: 15
            }, {
                label: '风险', name: 'danger', index: 'danger', width: 15
            },
            {
                label: '注册时间', name: 'createTime', index: 'createTime', width: 30,
                formatter: function (value) {
                    return transDate(value);
                }
            }]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        showDelData: true,
        title: null,
        user: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        // ruleValidateUid: {
        //     userId: [
        //         {required: true, message: '编号不能为空', trigger: 'blur'}
        //     ]
        // },
        q: {
            id: '',
            name: '',
            mobile: '',
            done: '',
            status: '',
            uid: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            showDelData = true;
            vm.title = "新增";
            vm.user = {};
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            showDelData = true;
            vm.title = "修改";
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.user.id == null ? "../user/save" : "../user/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.user),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        recharge: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.title = "修改";
            confirm('确定更改用户Done信息吗？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../user/updateDone/" + id,
                    async: true,
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });

            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../user/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        delUserData: function () {
            if (vm.q.uid == null) {
                return;
            }
            confirm('确定要删除用户数据？', function () {
                Ajax.request({
                    url: "../user/delData/",
                    params: {"uid": vm.q.uid},
                    type: "POST",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        delData: function () {
            vm.showList = true;
            vm.showDelData = false;
            vm.title = "删除数据";
            vm.user = {};
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../user/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.user = r.user;
                }
            });
        },
        clear: function () {
            Ajax.request({
                url: "../user/clear",
                async: true,
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                    });
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.showDelData = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'id': vm.q.id,
                    'name': vm.q.name,
                    'mobile': vm.q.mobile,
                    'done': vm.q.done,
                    'status': vm.q.status
                },
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                id: '',
                name: '',
                mobile: '',
                done: '',
                status: '',
                uid: '',
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleSubmitDel: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.delUserData()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        exportPro: function () {
            var strhref = "../user/export?name=" + vm.q.name;
            if (vm.q.id != '')
                strhref += "&id=" + vm.q.id;
            if (vm.q.mobile != '')
                strhref += "&phone=" + vm.q.mobile;
            if (vm.q.done != '')
                strhref += "&done=" + vm.q.done;
            if (vm.q.status != '')
                strhref += "&frozen=" + vm.q.status;
            let index = $("#jqGrid").jqGrid('getGridParam', 'page');
            strhref += "&index=" + index;
            strhref += "&size=" + 50;
            location.href = strhref;
        },
        handleSuccessImg: function (res, file) {
            vm.user.pictureUrl = file.response.url;
        },
        handleSuccessBg: function (res, file) {
            vm.user.bgimg = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        eyeImageImg: function () {
            var url = vm.user.pictureUrl;
            eyeImage(url);
        }, eyeImageBg: function () {
            var url = vm.user.bgimg;
            eyeImage(url);
        },
    }
});